//
// StoreNuke.Net -  http://www.storenuke.net
// Copyright (c) 2005
// by Robert J Collins ( info@storenuke.net ) of WillowTree Software, Inc. ( http://www.willowtreesoftware.com )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Web;

namespace StoreNuke.StarterKit.Commerce 
{

	public class Global : HttpApplication 
	{

		//*********************************************************************
		//
		// Application_BeginRequest Event
		//
		// The Application_BeginRequest method is an ASP.NET event that executes 
		// on each web request into the portal application.  The below method
		// obtains the current tabIndex and TabId from the querystring of the 
		// request -- and then obtains the configuration necessary to process
		// and render the request.
		//
		// This portal configuration is stored within the application's "Context"
		// object -- which is available to all pages, controls and components
		// during the processing of a single request.
		// 
		//*********************************************************************

		protected void Application_BeginRequest(Object sender, EventArgs e) 
		{
                
			try
			{
				if (Request.UserLanguages != null)
					Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages[0]);
				else
					// Default to English if there are no user languages
					Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");

				Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
			}
			catch (Exception ex)
			{
				Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");
			}
            
		}

		//*********************************************************************
		//
		// Application_Start
		//
		// Executes on the first web request into the application, 
		// when a new DLL is deployed, or when web.config is modified.
		// This procedure performs all version upgrade operations ( including database )
		//
		//*********************************************************************

		protected void Application_Start(object Sender, EventArgs E) 
		{  
			StoreGlobalDB db = new StoreGlobalDB();
			int intBuild = db.GetVersion();
			while (intBuild < FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductBuildPart) 
			{ 
				intBuild += 1; 

				if (File.Exists(Server.MapPath("DatabaseScripts\\1.0." + intBuild.ToString() + ".sql"))) 
					{ 
						StreamReader objStreamReader; 
						objStreamReader = File.OpenText(Server.MapPath("DatabaseScripts\\1.0." + intBuild.ToString() + ".sql")); 
						string strScript = objStreamReader.ReadToEnd(); 
						objStreamReader.Close(); 
						string strSQLExceptions = db.ExecuteSQLScript(strScript); 
						try 
						{ 
							StreamWriter objStream; 
							objStream = File.CreateText(Server.MapPath("DatabaseScripts\\1.0." + intBuild.ToString() + ".log")); 
							objStream.WriteLine(strSQLExceptions); 
							objStream.Close(); 
						} 
						catch 
						{ 
						} 
					} 
					else 
					{ 
						try 
						{ 
							StreamWriter objStream; 
							objStream = File.CreateText(Server.MapPath("DatabaseScripts\\1.0." + intBuild.ToString() + ".log")); 
							objStream.WriteLine("Upgrade Error. Could Not Locate " + Server.MapPath("DatabaseScripts\\1.0." + intBuild.ToString() + ".sql") + " Database Upgrade Script."); 
							objStream.Close(); 
						} 
						catch 
						{ 
						} 
						return; 
					} 
				db.AddVersion(intBuild); 
			}
		}
      
	}
}

